import store from '@/app/store'
import { get, update, code } from '@/api/erp/config'

// 初始化
const configStore = store()

// 加载
configStore.get = () => {
  return new Promise((resolve, reject) => {
    get().then(res => {
      configStore.setData(res)
      resolve(res)
    }).catch(err => {
      reject(err)
    })
  })
}

// 更新 
configStore.update = (data) => {
  return new Promise((resolve, jeject) => {
    update(Object.assign({}, configStore.getData(), data)).then(res => {
      configStore.get()
      resolve(res)
    }).catch(res => {
      jeject(res)
    })
  })
}

// 获取code
configStore.code = name => {
  return new Promise((resolve, jeject) => {
    code(name).then(res => {
      resolve(res.code)
    }).catch(res => {
      jeject(res)
    })
  })
}

// 小票限制金额
configStore.printNorm = money => {
  return Number(configStore.getData().print_norm) > Number(money)
}

// 批量折扣
configStore.orderDiscount = () => {
  return configStore.getData().orders.discount ?? 0
}

// 订单备注
configStore.orderRemarks = () => {
  return configStore.getData().orders.remarks ?? []
}

// 税率模式
configStore.taxType = () => {
  return configStore.getData().tax_type ?? 0
}

export default configStore